package com.gsbelarus.gedemin.salary.util;

import android.content.Context;

import com.gsbelarus.gedemin.salary.App;
import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.entity.model.DayEventModel;
import com.gsbelarus.gedemin.salary.entity.model.GdMsgHolidayModel;
import com.gsbelarus.gedemin.salary.entity.model.GdMsgModel;
import com.gsbelarus.gedemin.salary.entity.model.GdMsgVacationModel;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class ExtractStringHelper {

    private static Context context = App.getContext();

    public static String getGdMsgRequestStatus(GdMsgModel gdMsg) {
        String status = "";

        switch (gdMsg.getRequestStatus()) {
            case SUCCESS:
                status = context.getResources().getString(R.string.status_success) + ". ";

                if (gdMsg.getSubject() == GdMsgModel.Subject.INCOME) {
                    status = context.getResources().getString(R.string.status_success_income) + ". ";
                } else if (gdMsg.getSubject() == GdMsgModel.Subject.CHANGE_USER_PROFILE) {
                    status = context.getResources().getString(R.string.status_success_profile) + ". ";
                }
                break;
            case REJECTED:
                status = context.getResources().getString(R.string.status_rejected) + ". ";

                if (gdMsg.getSubject() == GdMsgModel.Subject.CHANGE_USER_PROFILE) {
                    status = context.getResources().getString(R.string.status_rejected_profile) + ". ";
                }
                break;
            case ERROR:
                status = context.getResources().getString(R.string.status_error) + ". ";
                break;
            case UNKNOWN:
                break;
        }
        return status;
    }

    public static String getGdMsgRequestSubject(GdMsgModel gdMsg) {
        String subject = "";
        switch (gdMsg.getSubject()) {
            case INCOME:
                subject = context.getResources().getString(R.string.income_statement);
                break;
            case HOLIDAY:
                subject = context.getResources().getString(R.string.request_holiday);
                break;
            case VACATION:
                subject = context.getResources().getString(R.string.request_vacation);
                break;
            case CHANGE_USER_PROFILE:
                subject = context.getResources().getString(R.string.change_personal_inf);
                break;
            case CHANGE_WORK_SCHEDULE:
                subject = context.getResources().getString(R.string.change_work_schedule);
                break;
            case INFO:
                subject = context.getResources().getString(R.string.info);
                break;
        }
        return subject;
    }

    public static String getGdMsgData(GdMsgModel gdMsg) {
        String msgData = "";
        if (gdMsg.getSender() == GdMsgModel.Sender.CLIENT) {
            String dateBegin;
            String dateEnd;
            SimpleDateFormat monthFormatter = new SimpleDateFormat("MMM yyyy", Locale.getDefault());
            SimpleDateFormat dateFormatter = new SimpleDateFormat("d MMMM");
            SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("d MMMM HH:mm");
            switch (gdMsg.getSubject()) {
                case INCOME:
                    dateBegin = monthFormatter.format(gdMsg.getGdMsgIncomeModel().getDateBegin().getTime());
                    dateEnd = monthFormatter.format(gdMsg.getGdMsgIncomeModel().getDateEnd().getTime());
                    msgData = context.getResources().getString(R.string.request_msg_period, dateBegin, dateEnd);
                    break;
                case HOLIDAY:
                    Calendar calendar = new GregorianCalendar();
                    calendar.setTime(gdMsg.getGdMsgHolidayModel().getDateBegin());
                    if (calendar.get(Calendar.HOUR) != 0) {
                        dateBegin = dateTimeFormatter.format(gdMsg.getGdMsgHolidayModel().getDateBegin().getTime());
                    } else {
                        dateBegin = dateFormatter.format(gdMsg.getGdMsgHolidayModel().getDateBegin().getTime());
                    }
                    calendar.setTime(gdMsg.getGdMsgHolidayModel().getDateEnd());
                    if (calendar.get(Calendar.HOUR) != 0) {
                        dateEnd = dateTimeFormatter.format(gdMsg.getGdMsgHolidayModel().getDateEnd().getTime());
                    } else {
                        dateEnd = dateFormatter.format(gdMsg.getGdMsgHolidayModel().getDateEnd().getTime());
                    }

                    msgData = context.getResources().getString(R.string.request_msg_period, dateBegin, dateEnd);
                    if (gdMsg.getGdMsgHolidayModel().getTypeIndex() == GdMsgHolidayModel.Type.VACATION.ordinal()) {
                        msgData += ". " + context.getResources().getString(R.string.request_holiday_type_vacation);
                    }
                    break;
                case VACATION:
                    dateBegin = dateFormatter.format(gdMsg.getGdMsgVacationModel().getDateBegin().getTime());
                    dateEnd = dateFormatter.format(gdMsg.getGdMsgVacationModel().getDateEnd().getTime());
                    msgData = context.getResources().getString(R.string.request_msg_period, dateBegin, dateEnd);

                    if (gdMsg.getGdMsgVacationModel().getTypeIndex() == GdMsgVacationModel.Type.REGULAR.ordinal()) {
                        msgData += " " + context.getResources().getString(R.string.request_vacation_type_regular);
                    } else if (gdMsg.getGdMsgVacationModel().getTypeIndex() == GdMsgVacationModel.Type.UNPAID.ordinal()) {
                        msgData += " " + context.getResources().getString(R.string.request_vacation_type_unpaid);
                    }
                    break;
                case CHANGE_USER_PROFILE:
                    msgData = context.getResources().getString(R.string.change_personal_inf_msg);
                    break;
            }
        } else if (gdMsg.getSender() == GdMsgModel.Sender.SERVER) {
            if (gdMsg.getRequestStatus() == GdMsgModel.RequestStatus.SUCCESS) {
                switch (gdMsg.getSubject()) {
                    case HOLIDAY:
                        Calendar begin = Calendar.getInstance();
                        Calendar end = Calendar.getInstance();
                        begin.setTime(gdMsg.getGdMsgHolidayModel().getDateBegin());
                        end.setTime(gdMsg.getGdMsgHolidayModel().getDateEnd());
                        begin.add(Calendar.DATE, 1);
                        if (!gdMsg.getMsg().isEmpty()) {
                            if (begin.compareTo(end) > 0)
                                msgData = context.getResources().getString(R.string.holiday_server_msg);
                            else
                                msgData = context.getResources().getString(R.string.vacation_server_msg);
                        }

                        break;
                    case VACATION:
                        if (!gdMsg.getMsg().isEmpty())
                            msgData = context.getResources().getString(R.string.vacation_server_msg);
                        break;
                }
            }
            msgData += gdMsg.getMsg();
        }

        return msgData;
    }

    public static String getGdMsgSender(GdMsgModel gdMsg) {
        String type = "";
        switch (gdMsg.getSender()) {
            case CLIENT:
                type = context.getString(R.string.msg_type_outgoing);
                break;
            case SERVER:
                type = context.getString(R.string.msg_type_inbox);
                break;
        }
        return type;
    }

    public static String gdMsgToString(GdMsgModel gdMsg) {
        if (gdMsg != null) {
            String result = getGdMsgSender(gdMsg) +
                    getGdMsgData(gdMsg) +
                    getGdMsgRequestSubject(gdMsg) +
                    getGdMsgRequestStatus(gdMsg) +
                    gdMsg.getComment() +
                    getGdMsgFormattedTimestamp(gdMsg) + getGdMsgLongFormattedTimestamp(gdMsg);

            if (gdMsg.getSender() == GdMsgModel.Sender.SERVER) {
                switch (gdMsg.getSubject()) {
                    case HOLIDAY:
                        result += gdMsgToString(gdMsg.getGdMsgHolidayModel().getRequestGdMsgModel());
                        break;
                    case VACATION:
                        result += gdMsgToString(gdMsg.getGdMsgVacationModel().getRequestGdMsgModel());
                        break;
                    case INCOME:
                        result += gdMsgToString(gdMsg.getGdMsgIncomeModel().getRequestGdMsgModel());
                        break;
                    case CHANGE_USER_PROFILE:
                        result += gdMsgToString(gdMsg.getGdMsgEmployeeModel().getRequestGdMsgModel());
                        break;
                    case CHANGE_WORK_SCHEDULE:
                        result += gdMsgToString(gdMsg.getGdMsgWorkScheduleModel().getRequestGdMsgModel());
                        break;
                    case INFO:
                        result += gdMsgToString(gdMsg.getGdMsgInfoModel().getRequestGdMsgModel());
                        break;
                }
            }
            return result.toLowerCase();
        } else {
            return "";
        }
    }

    // HH:mm
    public static String getFormatDurationDayEvent(DayEventModel dayEventModel) {
        Calendar duration = new GregorianCalendar();

        long diffInMillies = Math.abs(dayEventModel.getTimeEnd().getTime() - dayEventModel.getTimeBegin().getTime());
        duration.setTimeInMillis(diffInMillies);
        // calendar starts at 01.01.1970, 01:00, not at 00:00
        long hours = TimeUnit.HOURS.convert(diffInMillies, TimeUnit.MILLISECONDS);
        duration.set(Calendar.HOUR_OF_DAY, (int) hours);  //TODO <= 24

        if (duration.get(Calendar.MINUTE) > 0) {
            return new SimpleDateFormat("HH:mm").format(duration.getTime());
        } else {
            return new SimpleDateFormat("H").format(duration.getTime());
        }
    }

    //TODO вынести
    // отображение индикатора события на календаре
    public static boolean displayedInCalDayEvent(DayEventModel dayEventModel) {
        switch (dayEventModel.getEventType()) {
            case PUBLIC_HOLIDAY:
            case PRIVATE_HOLIDAY:
            case VACATION:
            case CHANGE_WORK_SCHEDULE:
                return true;
            default:
                return false;
        }
    }

    public static String getGdMsgFormattedTimestamp(GdMsgModel gdMsgModel) {
        Calendar currCalendar = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(gdMsgModel.getTimestamp());

        SimpleDateFormat dateFormat;
        // current year
        if (calendar.get(Calendar.YEAR) == currCalendar.get(Calendar.YEAR)) {
            // current day
            if (calendar.get(Calendar.DAY_OF_YEAR) == currCalendar.get(Calendar.DAY_OF_YEAR)) {
                dateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
            } else {
                dateFormat = new SimpleDateFormat("d MMM", Locale.getDefault());
            }
        } else {
            dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault());
        }

        return dateFormat.format(calendar.getTime());
    }

    public static String getGdMsgLongFormattedTimestamp(GdMsgModel gdMsgModel) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("d MMM yyyy, H:mm", Locale.getDefault());
        return dateFormat.format(gdMsgModel.getTimestamp().getTime());
    }
}
